let Component = require('./Component');
let comDriver = require('@/common/driver');

class BaseForm extends Component{
    constructor(param = {}) {
        super(param);

    }

    getDefaultParam() {
        return Object.assign({}, super.getDefaultParam(), {
            id: comDriver.createID(),
            controls: new Map(),
            short_keys: new Map(),
            need_login: false,
            auto_load: true,
            auto_call_back: true,
        });
    }

    async run(params) {
        if (params) {
            if (params.bean) {
                this._param.name = params.bean.name;
                if (params.bean.config) this._param = Object.assign(this._param, params.bean.config);
            }
            if (params.data) this._param = Object.assign(this._param, params.data);
        }
        this.param.prop = await comDriver.app().core.runBean((this.param.propName || this.param.name) + '.prop');
        if (this.param.prop && this.param.prop.run) this.param.prop.run({form: this, });

        this.loadControls();
        this.loadLayouts();
        await this.showContent();
        await this.build();

        await this.loadDefault();
        if (this.param.auto_load) await this.loadData();

        return params;
    }


    setCEnabled(id, value){
        let control = this.param.controls.get(id);
        if (!control) return;
        control.enabled = value;
    }

    getCEnabled(id){
        let control = this.param.controls.get(id);
        if (!control) return;

        return control.enabled;
    }

    setCVisible(id, value){
        let control = this.param.controls.get(id);
        if (!control) return;
        control.visible = value;
    }

    getCVisible(id){
        let control = this.param.controls.get(id);
        if (!control) return;

        return control.visible;
    }

    changeEnabled() {
        super.changeEnabled();
        this.param.controls.forEach(function (control, key, map) {
            control.enabled = this.enabled;
        }, this);
    }

    changeVisible() {
        super.changeVisible();
    }

    async showContent() {
        return this;
    }

    async build() {
        await super.build();

        await this.buildControl();
        this.buildLayout();
    }

    buildData(data) {
        if (this.param.prop && this.param.prop.buildData) this.param.prop.buildData(data);
    }

    async buildControl() {
        for (let [key, control] of this.param.controls) {
            await control.build(this);
        }
    }

    buildLayout() {
    }

    buildControlAfter() {
        this.param.controls.forEach(function (control, key, map) {
            control.buildAfter(this);
        }, this);

    }

    clear() {
        super.clear();

        this.param.controls.forEach(function (control, key, map) {
            control.clear();
        }, this);
    }

    loadControls() {
        if (this.param.prop && this.param.prop.loadControls) this.param.prop.loadControls();
    }

    loadLayouts() {

    }

    async loadDefault() {
        if (this.param.prop && this.param.prop.loadDefault) await this.param.prop.loadDefault();
    }

    getParam() {

    }

    async queryData(param) {
        return {};
    }

    async fillData(data){
        return data;
    }

    showData(data) {
        this.showControl(this.getControlData(data));
    }

    getControlData(data) {
        return data;
    }

    showControl(data) {
        for (let [key, control] of this.param.controls)
            if (control.showData)
                control.showData(data);
    }

    async loadData() {
        let param = this.getParam();
        this.showLoading('通讯中，请稍后...');
        try {
            let data = await this.queryData(param);
            data = await this.fillData(data);
            this.showData(data);
        } catch (e) {
            this.showError(e.message);
        } finally {
            this.hideLoading();
        }
    }

    async checkControls() {
        for (let [key, control] of this.param.controls)
            if (!await control.check())
                return false;
        return true;
    }

    async checkData() {
        if (!await this.checkControls()) return;
        return true;
    }

    packageData() {
        let data = {};
        this.packageControl(data);
        return data;
    }

    packageControl(data) {
        for (let [key, control] of this.param.controls)
            if (control.packageData)
                control.packageData(data);
    }

    async saveData(data) {
    }

    async saveAfter(result) {
    }

    async save() {
        let result = null;
        if (!await this.checkData()) return;
        let data = this.packageData();
        this.showLoading('通讯中，请稍后...');
        try {
            result = await this.saveData(data);
            await this.saveAfter(result);
        } catch (e) {
            this.showError(e.message);
            return;
        } finally {
            this.hideLoading();
        }
    }

    askClose(param) {
        return true;
    }

    trueClose(param) {
        this.closeForm();
        if (this.param.auto_call_back && this.param.callback) this.param.callback(param);
        return true;
    }

    closeForm() {
    }

    //关闭页面
    close(param) {
        if (!this.askClose(param)) return false;
        return this.trueClose(param);
    }

    get controls() {
        return this.param.controls;
    }

    control(id, value) {
        if (arguments.length === 2) {
            this.param.controls.set(id, value);
            return this;
        }
        else{
            return this.controls.get(id);
        }
    }

    addControl(value) {
        this.control(value.param.id, value);
        if (value.param.shortkey){
            this.param.shortkeys.set(value.param.shortkey, value.param.id);
        }
    }

    addLayout(value) {
        this.param.layouts.push(value);
    }

    value(id, value) {
        let control = this.controls.get(id);
        if (!control) return null;
        if (arguments.length === 2) {
            control.value = value;
        }
        else {
            return control.value;
        }
    }

    text(id, value) {
        let control = this.controls.get(id);
        if (!control) return null;
        if (arguments.length === 2) {
            control.text = value;
        }
        else {
            return control.text;
        }
    }

    extract(id) {
        if (arguments.length === 1) {
            let control = this.control(id);
            if (!control) return;
            control.extract();
        }
        else
            this.param.controls.forEach(function (control, key, map) {
                control.extract();
            }, this);
    }

    display(id) {
        if (arguments.length === 1) {
            let control = this.control(id);
            if (!control) return;
            control.display();
        }
        else
            this.param.controls.forEach(function (control, key, map) {
                control.display();
            }, this);
    }

    elementText(name) {
        return name;
    }

    element(name) {
        return null;
    }

    showLoading(message){

    }

    hideLoading(){

    }

    //显示信息
    showTip(message, duration = 3, onClose = null){

    }

    //显示通知
    showNotice(message, title = '通知'){

    }

    async showInput(title) {
    }

    //遮盖屏幕，需要点确定
    showMessage(message, duration = 3, onClose = null){

    }

    //错误信息
    showError(message, duration = 3, onClose = null){

    }

    //询问，返回bool
    async showAsk(message){

    }

    async handleEvent(eventID, param){
    }

}


module.exports = BaseForm;
